require 'config/environment'

task :read_xml do
  raise "IO Error: Please specify xml file(UTF-8)" unless ENV.has_key? "doc"
  require 'xml/libxml'
  require 'lib/word_set'
  
  doc = XML::Document.file(ENV["doc"])
  doc.find('//corpus/entry').each do |entry|
    # cashe data
    list = Hash.new{|hash,key| hash[key] = Array.new }
    texts = Hash.new{|hash,key| hash[key] = "" }
    entry.find("sense").each do |sense|
      sense_id = sense["id"][/^\d+-\d+-\d+-\d+/]
      sense.find("mor").each do |mor|
        mor.find("ruby").each do |ruby|
          next unless mor.to_s.match(">(.+)</")[1].include?("<ruby")
          pre_text = mor.to_s.match(">(.+)</")[1].sub(/<ruby.+$/,"")
          list[sense_id] << {"surface" => pre_text}
          texts[sense_id] += pre_text
        end
        #
        word = Hash.new
        word["surface"] = mor.to_s.match(">([^><]+?)</")[1] #node.content がうまく動作しないので仕方なく正規表現で獲得
        word["reading"] = mor["rd"] unless mor["rd"].nil?
        list[sense_id] << word
        texts[sense_id] += word["surface"]
        #
        mor.find("ruby").each do |ruby|
          list[sense_id].concat [{"surface" => "（"},{"surface" => ruby["rd"]},{"surface" => "）"}]
          texts[sense_id] += "（#{ruby["rd"]}）"
          next unless mor.to_s.match(">(.+)</")[1].include?("<ruby")
          post_text = mor.to_s.match(">(.+)</")[1].sub(/^.+<\/ruby>/,"")
          list[sense_id] << {"surface" => post_text}
          texts[sense_id] += post_text
        end
      end
    end
    
    # save data
    sense_details = Array.new
    senses = Array.new
    list.each_with_index do |item,index|
      id = item.first
      sense = item.last
      senses << sense
      sense_details << SenseDetail.create(:tid => entry["id"].to_i,:text => texts[id],:position => index)
    end
    unless entry["knz"].nil?
      entries = entry["knz"].split("・")
      entries.each do |item|
        WordSet.all_patterns_of(item).each do |pattern|
          dic = Dictionary.create(:index => pattern,:entry => {"surface" => pattern,"reading" => entry["mds"]},:sense => senses)
          dic.sense_details << sense_details
        end
      end
    end
    dic = Dictionary.create(:index => entry["mds"],:entry => {"surface" => entry["mds"]},:sense => senses)
    dic.sense_details << sense_details
  end
  
end